/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Chat;

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Frame;
import java.awt.Label;
import java.awt.Panel;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.List;
import java.util.List;

/**
 *
 * @author snake_gt
 */
public class ClienteInterfaz {
    private TextArea area;
    private Button boton;
    private TextField campo;
    private Label etiqueta;
    private Frame ventana;
    private Socket cliente;
    private  List lista;
    public ClienteInterfaz() {
        area= new TextArea();
        boton = new Button("Enviar");
        campo = new TextField();
        etiqueta = new Label("criis");
        ventana= new Frame();
        ventana.add(etiqueta,"North");
        ventana.add(area);
        Panel p = new Panel();
        p.setLayout(new BorderLayout());
        p.add(campo);
        p.add(boton,"East");
        ventana.add(p,"South");
      ventana.add(List);
        ventana.setSize(400,400);
        ventana.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e){
                System.exit(0);
            }
        });
        boton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                try {
                    DataOutputStream out = new DataOutputStream(cliente.getOutputStream());
                    out.writeUTF(campo.getText());
                    campo.setText("");
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        });
    }

    public void iniciar(){
        ventana.setVisible(true);
        try {
            cliente = new Socket("localhost", 8989);
            DataOutputStream out=new DataOutputStream(cliente.getOutputStream());
            out.writeUTF(etiqueta.getText());
            area.setText("Conectado ...");
            Thread t = new Thread(new HiloCliente(cliente,area));
            t.start();
        } catch (UnknownHostException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }



    public static void main(String[] args) {
        ClienteInterfaz ci = new ClienteInterfaz();
        ci.iniciar();
    }

}
